package com.yun.config;

import com.yun.api.IHelloService1;
import com.yun.api.impl.HelloServiceImpl1;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * @author: Mr.Liu
 * @version: 1.0.0
 * @description: 启动自动配置
 * @create: 2022-07-19 14:48
 * @modify: 2022-07-19 14:48
 **/
@Configuration
@EnableConfigurationProperties(HelloServiceProperties.class)
@ConditionalOnClass(IHelloService1.class)
/**
 *@ConditionalOnProperty来控制Configuration是否生效，从application.properties中读取某个属性值，
 * 如果该值为空，默认值为true
 */
@ConditionalOnProperty(prefix = "hello",value = "enabled",matchIfMissing = true)
public class HelloServiceAutoConfiguration {
    @Autowired
    private HelloServiceProperties helloServiceProperties;

    @Bean
    @ConditionalOnMissingBean(IHelloService1.class)
    public IHelloService1 helloService(){
        IHelloService1 helloService = new HelloServiceImpl1();
        helloService.setMsg(helloServiceProperties.getMsg());
        return helloService;
    }

}
